<script lang="ts">
import { defineComponent, reactive, ref, onMounted, watch } from 'vue'
import { Pagination, FreeMode } from 'swiper'
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css'
import 'swiper/css/pagination'
import 'swiper/css/free-mode'
import router from "@/router";
import dayjs from 'dayjs'
import resizeRank from '@/util/screen'

export default defineComponent({
  props: ['newsList', 'rank'],
  name: 'swiper-example-free-mode',
  title: 'Free mode / no fixed positions',
  url: import.meta.url,
  components: {
    Swiper,
    SwiperSlide
  },
  emits: ['touchMove'],
  setup(props, context) {
    const NewsList = ref([]) as any
    NewsList.value = props.newsList
    watch(() => props.newsList, (newValue) => {
      NewsList.value = props.newsList

    })
    const checkIndex = ref<number>(0)
    const rank = ref(0) /* 1为手机端 */
    // const NewsList = props.newsList
    let data = reactive({
      swiperType: false,
    })
    rank.value = props.rank
    onMounted(() => {
      /**
       * @methods 判定pc&movie
       */
      rank.value = resizeRank(rank.value)
      window.addEventListener('resize', () => {
        let screenWidth = document.body.clientWidth
        if (screenWidth < 768) {
          rank.value = 1
        } else {
          rank.value = 0
        }
      })
    })

    NewsList.value.forEach((item: any, index: any) => {
      const timeZone = ref(0) /* 用户当前时区 */
      timeZone.value = -new Date().getTimezoneOffset() / 60
      item.publish_time = item.publish_time - (8 - (timeZone.value)) * 3600
      const year = dayjs(item.publish_time * 1000).year()
      const month = dayjs(item.publish_time * 1000).month() + 1
      const day = dayjs(item.publish_time * 1000).date()
      item.newsDate = `${year}-${month}-${day}`
    })

    const toCheckSulution = (item: any, index: number) => {
      item.check = true
      checkIndex.value = index
    }

    const touchMove = (swiper: any, event: any) => {
      context.emit('touchMove')
    }


    const onMouseEnter = (item: any) => {
      item.check = true
    }
    const onMouseLeave = (item: any) => {
      item.check = false
    }
    const LearnDatas = (id: number) => {
      router.push({
        path: '/NewsDetail',
        query: {
          id: id,
        },
      });
    };
    return {
      LearnDatas,
      onMouseEnter,
      onMouseLeave,
      data,
      toCheckSulution,
      checkIndex,
      touchMove,
      rank,
      NewsList,
      modules: [Pagination, FreeMode],
    }
  }
})
</script>
    
<template>
  <div class="solution">
    <div class="solutionRight">
      <swiper class="swiper" :modules="modules" :space-between="25" :slides-per-view="rank == 0 ? 3 : 1"
        @touchMove="touchMove" :free-mode="true">
        <swiper-slide class="slide" v-for="(item, index) in NewsList" :key="index"
          :class="item.check ? 'slideCheck' : 'slide'" @click="toCheckSulution(item, index)"
          @mouseenter="onMouseEnter(item)" @mouseleave="onMouseLeave(item)">
          <el-card shadow="never" style="border-radius: 15px;border:none"
            @click="LearnDatas(item.id)">
            <div class="box_item scaleImgCard">
              <div class="imgs_b"><img :src="item.cover_image" alt=""></div>
              <div class="item_b">
                <div class="time auxiliaryText contentText" v-if="item.publish_time">{{ item.newsDate }}</div>
                <div class="text contentText">{{ item.title }}
                </div>
                <el-button color="#064DD6" size="large" round @click="LearnDatas(item.id)">点击查看</el-button>
              </div>
            </div>
          </el-card>
        </swiper-slide>
      </swiper>
    </div>
  </div>
</template>
    
<style lang="scss" scoped>
:deep(.box_item) {
  .btn {
    background: $mainColor;
    color: #fff !important;
    transition: all 0.3s;

    span {
      text-decoration: initial !important;
    }
  }
}

.solution {
  margin-top: 50px;
  display: flex;
  width: 100%;
  overflow: hidden;
  // padding: 0 20px;



  .solutionRight {
    width: 100%;

    .swiper {
      overflow: initial;

      :deep(.swiper-pagination) {
        bottom: -30px !important;
      }
    }

    .imgs_b {
      height: 200px;
      overflow: hidden;
      border-radius: 10px;

      img {
        width: 100%;
        height: 100%;
        //css 图片自适应
        object-fit: cover;
      }
    }

    .item_b {
      .time {
        // font-size: $auxiliary;
        // color: $mainColor;
        margin: 20px 0 10px 0;
      }

      .text {
        // font-size: $secondTitle;
        margin-bottom: 20px;
        word-break: break-all;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
      }

    }


    .ImgCheck {
      border-radius: 15px;
      overflow: hidden;
      // box-shadow: 5px 5px 5px 0 rgba($color: #999, $alpha: .3);
      margin: 10px 0;
      padding: 10px;
    }

    .ImgNoCheck {
      border-radius: 15px;
      overflow: hidden;
      margin: 10px 0;
      padding: 10px;
    }
  }
}

@media screen and (max-width:450px) {
  .solution {
    padding: 0px;
  }

  .item_b {
    .el-button {
      background-color: $mainColor;
      color: #fff !important;


    }
  }

  span {
    text-decoration: auto !important;
  }

  .swiper {
    .slide {
      // padding-right: 30px;
      box-shadow: none !important;
      border-radius: $cardRadius;
    }
  }

  /* :deep(.el-card__body){
    padding: 0 !important;
  } */
}
</style>